<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>
    let obj = {
      name: "wsh"
    }
    function fn(age, type) {
      return {
        name: this.name,
        age,
        type
      }
    }
    let newBind = function(obj) {
      // arr截取第一位之后的,第一位为this指向的对象
      let that = this, arr = Array.prototype.slice.call(arguments, 1)

      let newFn = function() {
        let arr2 = Array.prototype.slice.call(arguments)
        let list = arr.concat(arr2)
        if(this instanceof newFn) {
          // 构造函数的this指向当前实例(this.__proto__ === newFun.prototype)
          // 如果当前this.__proto__等于newFun.prototype
          return that.call(this, ...list)
        } else {
          // that指向调用newBind的函数
          return that.call(obj, ...list)
        }
      }
      return newFn
    }
    Function.prototype.newBind = newBind
    // fn.bind(obj)()
    // 此时this指向window,fn函数中的name为undefined
    // new fn.bind(obj)
    let fn1 = fn.newBind(obj, 18)
    console.log(fn1("vip"));
  </script>
</body>
</html>